﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using intelliPaper.Model;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for AddHistoryMessage.xaml
    /// </summary>
    public partial class AddHistoryMessage : Window
    {
        static AddHistoryMessage ahm;
        public static object ShowDialogue(Window sender, object dtctx)
        {
            ahm = new AddHistoryMessage();
            ahm.ShowInTaskbar = false;
            ahm.Owner = sender;
            ahm.Height = (sender).ActualHeight;
            ahm.Width = (sender).ActualWidth;
            ahm.SetWindowStatus();
            ahm.DataContext = dtctx;
            sender.Topmost = true;
            ahm.ShowDialog();
            ahm.RestoreWindowStatus();
            return ahm.DialogueResult;
        }
        void RestoreWindowStatus()
        {
            this.Owner.Effect = null;
            this.Owner.Topmost = false;
        }
        void SetWindowStatus()
        {
            (Resources["OnLoaded1"] as System.Windows.Media.Animation.Storyboard).Completed += new EventHandler(ErrorMessage_Completed);
            (Resources["OnLoaded1"] as System.Windows.Media.Animation.Storyboard).Begin();
        }
        void ErrorMessage_Completed(object sender, EventArgs e)
        {
            if (ahm == null) return;
            Owner.Effect = new System.Windows.Media.Effects.BlurEffect() { Radius = 10 };
        }
        public IEnumerable<DateTime> AvailableDateTimeCollection;
        public WindowCommandResult DialogueResult
        {
            private set;
            get;
        }
        static ValidationLogic[] validationLogics;
        HistoryMessage hm;
        static AddHistoryMessage()
        {
            validationLogics = new ValidationLogic[]
            {
                new ValidationLogic()
                {
                    Detail = "错误率为非负且在[0,1]之间",
                    PreventContinue=true,
                    Summary="错误数值",
                    ValidateMethod = ValidationLogic.Double01Range
                },
                new ValidationLogic()
                {
                    Detail = "无效的日期格式",
                    PreventContinue= true,
                    Summary = "日期格式错误",
                    ValidateMethod = ValidationLogic.DateTimeFormatLogic
                }
            };
        }
        public AddHistoryMessage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AddHistoryMessage_Loaded);
            validationLogics[0].FrameworkElement = ContentBox;
            validationLogics[1].FrameworkElement = datePicker;
            //this.Closing += new System.ComponentModel.CancelEventHandler(AddHistoryMessage_Closing);
        }

        //void AddHistoryMessage_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        //{
        //    try
        //    {
        //        this.Owner.Effect = null;
        //    }
        //    catch
        //    {
        //    }
        //}
        void AddHistoryMessage_Loaded(object sender, RoutedEventArgs e)
        {
            hm = this.DataContext as HistoryMessage;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            foreach (ValidationLogic vl in validationLogics)
            {
                if (!vl.ValidateMethod(vl.FrameworkElement))
                {
                    Message.Show(this, "", MessageType.Comfirm, vl.Detail, vl.Summary);
                    return;
                }
            }
            hm.RecordDate = DateTime.Parse(datePicker.Text);
            hm.ErrorRate = (int)(100 * double.Parse(ContentBox.Text));
            DialogueResult = WindowCommandResult.CreateNewSavingChanges;
            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogueResult = WindowCommandResult.CreateNewDiscardingChanges;
            this.Close();
        }
    }
}
